package com.excesys.exsecs.access.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.access.entity.AccessProduct;
import com.excesys.exsecs.access.entity.AccessThingModel;
import com.excesys.exsecs.access.mapper.AccessProductMapper;
import com.excesys.exsecs.access.mapper.AccessThingModelMapper;
import com.excesys.exsecs.access.service.AccessProductService;
import com.excesys.exsecs.access.service.AccessThingModelService;
import com.excesys.exsecs.access.vo.AccessDeviceInfoVO;
import com.excesys.exsecs.access.vo.AccessProductVO;
import com.excesys.exsecs.access.vo.AccessThingModelVO;
import com.excesys.exsecs.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 物模型表
 *
 * @author lxc
 * @date 2024-05-06 17:44:02
 */
@Service
@RequiredArgsConstructor
public class AccessThingModelServiceImpl extends ServiceImpl<AccessThingModelMapper, AccessThingModel> implements AccessThingModelService {

	private final AccessProductMapper accessProductMapper;
	@Override
	public AccessThingModel getThingModelByProductKey(String productKey) {
		AccessThingModel thing = this.getOne(Wrappers.<AccessThingModel>lambdaQuery().eq(AccessThingModel::getProductKey, productKey));
		if (ObjectUtils.isNotEmpty(thing)){
			AccessProduct product = accessProductMapper.selectOne(Wrappers.<AccessProduct>lambdaQuery().eq(AccessProduct::getProductKey, productKey));
			thing.setProductName(product.getName());
		}
		return thing;
	}

	@Override
	public boolean saveModel(AccessThingModel accessThingModel) {
		System.out.println(accessThingModel.toString());
		long count = this.count(Wrappers.<AccessThingModel>lambdaQuery().eq(AccessThingModel::getProductKey, accessThingModel.getProductKey()));
		if (count == 0){
			return this.save(accessThingModel);
		} else {
			return this.update(Wrappers.<AccessThingModel>lambdaUpdate().set(AccessThingModel::getModel, JSONUtil.toJsonStr(accessThingModel.getModel()))
					.eq(AccessThingModel::getProductKey, accessThingModel.getProductKey()));
		}
	}

	@Override
	public R importProduct(List<AccessThingModelVO> excelVOList, String productKey, BindingResult bindingResult) {
		// 执行数据插入操作
		for (AccessThingModelVO excel : excelVOList) {
			AccessThingModel model = new AccessThingModel();
			BeanUtil.copyProperties(excel, model);
			model.setProductKey(productKey);
			try {
				//插入物模型
				if (ObjectUtils.isNotEmpty(excel.getModel())){
					JSONObject modelJson = JSONUtil.parseObj(excel.getModel());
					model.setModel(modelJson);
					long count = this.count(Wrappers.<AccessThingModel>lambdaQuery().eq(AccessThingModel::getProductKey, productKey));
					if (count > 0){
						this.update(Wrappers.<AccessThingModel>lambdaUpdate().set(AccessThingModel::getModel, JSONUtil.toJsonStr(model.getModel()))
								.eq(AccessThingModel::getProductKey, model.getProductKey()));
					}else {
						this.save(model);
					}
				}
			} catch (Exception e){
				return R.failed("物模型添加失败");
			}
		}
		return R.ok("本次导入成功");
	}

	@Override
	public List<AccessThingModelVO> exportThingModel(AccessThingModel accessThingModel) {
		List<AccessThingModel> list = this.list(Wrappers.<AccessThingModel>lambdaQuery()
				.eq(StringUtils.isNotEmpty(accessThingModel.getProductKey()), AccessThingModel::getProductKey, accessThingModel.getProductKey()));
		if (CollectionUtils.isNotEmpty(list)){
			list.forEach(it ->{
				AccessProduct product = accessProductMapper.selectOne(Wrappers.<AccessProduct>lambdaQuery().eq(AccessProduct::getProductKey, it.getProductKey()));
				it.setProductName(product.getName());
			});
		}
		return list.stream().map(model -> {
			AccessThingModelVO excelVO = new AccessThingModelVO();
			if (ObjectUtils.isNotEmpty(model.getModel())){
				excelVO.setModel(JSONUtil.toJsonStr(model.getModel()));
			}
			BeanUtils.copyProperties(model, excelVO);
			return excelVO;
		}).collect(Collectors.toList());
	}
}